#include "steganographie.h"

Image toLsd44(Image porteuse, Image cachee)
{
    Image retour = Image();
    retour.nom = ((QString)((QString)(porteuse.getNom().c_str()) + "(marquee)")).toStdString();
    retour.motClef = porteuse.motClef;
    retour.qImage = porteuse.qImage;
    int i,j,r,v,b;
    QRgb pixP,pixC, color;

    for(i=0;i<retour.qImage.width();i++)
    {
        for(j=0;j<retour.qImage.height();j++)
        {
            pixP=porteuse.qImage.pixel(i,j);
            pixC=cachee.qImage.pixel(i,j);
            r=(((qRed(pixP))>>4)<<4)+(qRed(pixC)>>4);
            v=(((qGreen(pixP))>>4)<<4)+(qGreen(pixC)>>4);
            b=(((qBlue(pixP))>>4)<<4)+(qBlue(pixC)>>4);
            color=qRgb(r, v, b);

            retour.qImage.setPixel(i,j,color);
        }
    }

    QString fileName =
                    QFileDialog::getSaveFileName(0, "Sauvegarder image marquee", QDir::currentPath() + "/image");
    if (!fileName.isEmpty())
    {
            QFileInfo fi(fileName);
            retour.nom = fi.fileName().toStdString();
            retour.chemin = fileName.toStdString();

            retour.qImage.save(fileName,"JPG");
    }

    retour.calculHistogram();
    return retour;
}

void fromLsd44(Image source, Image * porteuse, Image * cachee)
{
    int i,j,rp,rc,vp,vc,bp,bc;
    QRgb pix;

    porteuse->nom=((QString)((QString)(source.getNom().c_str()) + "(porteuse)")).toStdString();
    cachee->nom=((QString)((QString)(source.getNom().c_str()) + "(cachee)")).toStdString();

    porteuse->qImage = source.qImage;
    cachee->qImage = source.qImage;

    for(i=0;i<source.qImage.width();i++)
    {
        for(j=0;j<source.qImage.height();j++)
        {
            pix=source.qImage.pixel(i,j);
            rp=((qRed(pix)>>4)<<4);
            rc=((qRed(pix)%16)<<4);
            vp=((qGreen(pix)>>4)<<4);
            vc=((qGreen(pix)%16)<<4);
            bp=((qBlue(pix)>>4)<<4);
            bc=((qBlue(pix)%16)<<4);
            porteuse->qImage.setPixel(i,j,qRgb(rp, vp, bp));
            cachee->qImage.setPixel(i,j,qRgb(rc, vc, bc));
        }
    }

    QString fileName =
                    QFileDialog::getSaveFileName(0, "Sauvegarder image porteuse", QDir::currentPath() + "/image");
    if (!fileName.isEmpty())
    {
            QFileInfo fi(fileName);
            porteuse->nom = fi.fileName().toStdString();
            porteuse->chemin = fileName.toStdString();

            porteuse->qImage.save(fileName,"JPG");
    }

    fileName =
                    QFileDialog::getSaveFileName(0, "Sauvegarder image cachee", QDir::currentPath() + "/image");
    if (!fileName.isEmpty())
    {
            QFileInfo fi(fileName);
            cachee->nom = fi.fileName().toStdString();
            cachee->chemin = fileName.toStdString();

            cachee->qImage.save(fileName,"JPG");
    }
}

Image toLsd(Image porteuse, Image cachee)
{
    Image retour = Image();
    retour.nom = ((QString)((QString)(porteuse.getNom().c_str()) + "(marquee)")).toStdString();
    retour.motClef = porteuse.motClef;
    retour.qImage = porteuse.qImage;
    int i,j,x,y,r,v,b;
    QRgb pixP,pixC, color;

    /*
      le premier pixel (0,0) de l'image marquée contient la largeur de l'image cachée (rrrr|vvvv|bbbb)
      le second pixel (0,1) de l'image marquée contient la hauteur de l'image cachée (idem)
    */
    int cacheeWidth = cachee.qImage.width();
    int cacheeHeight = cachee.qImage.height();

    pixP=porteuse.qImage.pixel(0,0);
    r=(((qRed(pixP))>>4)<<4)+ ((cacheeWidth%4096)>>8);
    v=(((qGreen(pixP))>>4)<<4)+((cacheeWidth%256)>>4);
    b=(((qBlue(pixP))>>4)<<4)+(cacheeWidth%16);
    color=qRgb(r, v, b);

    retour.qImage.setPixel(0,0,color);

    pixP=porteuse.qImage.pixel(0,1);
    r=(((qRed(pixP))>>4)<<4)+ ((cacheeHeight%4096)>>8);
    v=(((qGreen(pixP))>>4)<<4)+((cacheeHeight%256)>>4);
    b=(((qBlue(pixP))>>4)<<4)+(cacheeHeight%16);
    color=qRgb(r, v, b);

    retour.qImage.setPixel(0,1,color);

    //puis on remplit
    i=0;
    j=2;
    for(x=0;x<cacheeWidth;x++)
    {
        for(y=0;y<cacheeHeight;y++)
        {
            pixP=porteuse.qImage.pixel(i,j);
            pixC=cachee.qImage.pixel(x,y);
            r=(((qRed(pixP))>>4)<<4)+(qRed(pixC)>>4);
            v=(((qGreen(pixP))>>4)<<4)+(qGreen(pixC)>>4);
            b=(((qBlue(pixP))>>4)<<4)+(qBlue(pixC)>>4);
            color=qRgb(r, v, b);

            retour.qImage.setPixel(i,j,color);

            j++;
            if(j==porteuse.qImage.height())
            {
                i++;
                j=0;
            }
        }
    }

    QString fileName =
                    QFileDialog::getSaveFileName(0, "Sauvegarder image marquee", QDir::currentPath() + "/image");
    if (!fileName.isEmpty())
    {
            QFileInfo fi(fileName);
            retour.nom = fi.fileName().toStdString();
            retour.chemin = fileName.toStdString();

            retour.qImage.save(fileName,"JPG");
    }

    retour.calculHistogram();
    return retour;
}

void fromLsd(Image source, Image * porteuse, Image * cachee)
{
    int i,j,rp,rc,vp,vc,bp,bc,x,y;
    QRgb pix;

    porteuse->nom=((QString)((QString)(source.getNom().c_str()) + "(porteuse)")).toStdString();
    cachee->nom=((QString)((QString)(source.getNom().c_str()) + "(cachee)")).toStdString();

    porteuse->qImage = source.qImage;

    /*récupérer la taille de l'image cachée sur les 2 premiers pixels*/
    int cacheeWidth;
    int cacheeHeight;

    pix=source.qImage.pixel(0,0);
    cacheeWidth=((qRed(pix)%16)<<8) + ((qGreen(pix)%16)<<4) + (qBlue(pix)%16);

    pix=source.qImage.pixel(0,1);
    cacheeHeight=((qRed(pix)%16)<<8) + ((qGreen(pix)%16)<<4) + (qBlue(pix)%16);

    cachee->qImage = QImage(cacheeWidth,cacheeHeight,QImage::Format_RGB32);

    //remplir
    i=0;
    j=2;
    for(x=0;x<cacheeWidth;x++)
    {
        for(y=0;y<cacheeHeight;y++)
        {
            pix=source.qImage.pixel(i,j);
            rp=((qRed(pix)>>4)<<4);
            rc=((qRed(pix)%16)<<4);
            vp=((qGreen(pix)>>4)<<4);
            vc=((qGreen(pix)%16)<<4);
            bp=((qBlue(pix)>>4)<<4);
            bc=((qBlue(pix)%16)<<4);
            porteuse->qImage.setPixel(i,j,qRgb(rp, vp, bp));
            cachee->qImage.setPixel(x,y,qRgb(rc, vc, bc));

            j++;
            if(j==source.qImage.height())
            {
                i++;
                j=0;
            }
        }
    }

    x=i;
    y=j;

    for(i=x;i<source.qImage.width();i++)
    {
        for(j=y;j<source.qImage.height();j++)
        {
            pix=source.qImage.pixel(i,j);
            rp=((qRed(pix)>>4)<<4);
            rc=((qRed(pix)%16)<<4);
            vp=((qGreen(pix)>>4)<<4);
            vc=((qGreen(pix)%16)<<4);
            bp=((qBlue(pix)>>4)<<4);
            bc=((qBlue(pix)%16)<<4);
            porteuse->qImage.setPixel(i,j,qRgb(rp, vp, bp));
        }
    }

    QString fileName =
                    QFileDialog::getSaveFileName(0, "Sauvegarder image porteuse", QDir::currentPath() + "/image");
    if (!fileName.isEmpty())
    {
            QFileInfo fi(fileName);
            porteuse->nom = fi.fileName().toStdString();
            porteuse->chemin = fileName.toStdString();

            porteuse->qImage.save(fileName,"JPG");
    }

    fileName =
                    QFileDialog::getSaveFileName(0, "Sauvegarder image cachee", QDir::currentPath() + "/image");
    if (!fileName.isEmpty())
    {
            QFileInfo fi(fileName);
            cachee->nom = fi.fileName().toStdString();
            cachee->chemin = fileName.toStdString();

            cachee->qImage.save(fileName,"JPG");
    }
}
